#include "comm.hpp"

#define ERR_EXIT(m)         \
    do                      \
    {                       \
        perror(m);          \
        exit(EXIT_FAILURE); \
    } while (0) 

int main()
{
    umask(0);
    int n = mkfifo(FIFO_NAME, 0666);
    if (n < 0)
    {
        ERR_EXIT("mkfifo error");
    }
    int rfd=open(FIFO_NAME,O_RDONLY);
    if(rfd==-1)
        ERR_EXIT("open FIFO error");
    int wfd=open("dest.txt",O_WRONLY|O_TRUNC|O_CREAT,0666);
    if(wfd==-1)
        ERR_EXIT("open dest.txt error");
    char buffer[1024];
    while(true)
    {
        int n=read(rfd,buffer,sizeof(buffer));
        if(n<0)
            ERR_EXIT("read error");
        else if(n==0)
        {
            std::cout<<"拷贝完毕"<<std::endl;
            break;
        }
        write(wfd,buffer,n);
    }
    close(rfd);
    close(wfd);
    unlink(FIFO_NAME);
    return 0;
}